package com.ceshiren.springstudy.controller;

import com.ceshiren.springstudy.dto.UserDto;
import com.ceshiren.springstudy.service.ResultService;
import com.ceshiren.springstudy.util.Result;
import com.ceshiren.springstudy.util.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "定义统一返回的结果类")
public class ResultController {

    @Autowired
    ResultService resultService;

    @PostMapping(value = "/q/login",produces = "application/json")
    @ApiOperation("用户登录方法")
    public Result login(@RequestBody UserDto userDto){
        Boolean login = resultService.login(userDto);
        if(login){
            return Result.success().data(userDto).message("用户登录成功");
        }
        return Result.error().message("用户登录失败");
    }

    @PostMapping(value = "/regist/error")
    @ApiOperation("测试异常统一处理")
    public Result exceptionTest(@RequestBody UserDto userDto){
        int a = 10/0;
        return Result.error();
    }


    @PostMapping(value = "/regist/error2")
    @ApiOperation("测试异常统一处理")
    public Result exceptionTest2(@RequestBody UserDto userDto){
        throw new ServiceException("自定义异常处理类",10086);
//        return Result.error();
    }


}
